package com.jy.appmarket.utils;

import java.lang.ref.SoftReference;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.jy.appmarket.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

public class LoadImageUtils {
	// private static final String TAG = "LoadImageUtils";

	private static SoftReference<Drawable> sList;
	private static SoftReference<Drawable> sBanner;
	private static SoftReference<Drawable> sApkImage;
	private static SoftReference<Drawable> sGroupTop;
	private static SoftReference<Drawable> sCateImage;

	private static Drawable getCategroyIconDrawable(Context context) {
		if (sCateImage != null && sCateImage.get() != null) {
			// LogUtil.d(TAG, "list icon hit");
			return sCateImage.get();
		}

		int h = context.getResources().getDimensionPixelSize(R.dimen.catgroy_img_height);
		int w = context.getResources().getDimensionPixelSize(R.dimen.catgroy_img_width);
		int radius = context.getResources().getDimensionPixelSize(
				R.dimen.download_button_in_list_radius);

		Drawable d = getLoadingDrawable(context, w, h, radius,
				R.drawable.default_loading_icon_small);
		if (sCateImage == null || sCateImage.get() == null) {
			sCateImage = new SoftReference<Drawable>(d);
		}
		return d;
	}

	public static DisplayImageOptions getCategroyIconDisplayOption(Context context) {
		if (context == null) {
			return null;
		}
		Drawable d = getCategroyIconDrawable(context);
		if (d == null) {
			return null;
		}
		return new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
				.resetViewBeforeLoading(false).showImageOnLoading(d).build();
	}

	public static DisplayImageOptions getListIconDisplayOption(Context context) {
		if (context == null) {
			return null;
		}
		Drawable d = getListIconDrawable(context);
		if (d == null) {
			return null;
		}
		return new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
				.resetViewBeforeLoading(false).showImageOnLoading(d).build();
	}

	private static Drawable getListIconDrawable(Context context) {
		if (sList != null && sList.get() != null) {
			// LogUtil.d(TAG, "list icon hit");
			return sList.get();
		}

		int h = context.getResources().getDimensionPixelSize(R.dimen.list_app_icon_height);
		int w = context.getResources().getDimensionPixelSize(R.dimen.list_app_icon_width);
		int radius = context.getResources().getDimensionPixelSize(
				R.dimen.download_button_in_list_radius);

		Drawable d = getLoadingDrawable(context, w, h, radius, R.drawable.default_loading_icon);
		if (sList == null || sList.get() == null) {
			sList = new SoftReference<Drawable>(d);
		}
		return d;
	}

	public static DisplayImageOptions getGroupTopIconDisplayOption(Context context) {
		if (context == null) {
			return null;
		}
		Drawable d = getGroupTopIconDrawable(context);
		if (d == null) {
			return null;
		}

		return new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
				.resetViewBeforeLoading(false).showImageOnLoading(d).build();
	}

	private static Drawable getGroupTopIconDrawable(Context context) {
		if (sGroupTop != null && sGroupTop.get() != null) {
			// LogUtil.d(TAG, "list icon hit");
			return sGroupTop.get();
		}

		int h = context.getResources().getDimensionPixelSize(R.dimen.group_top_item_height);
		int w = context.getResources().getDimensionPixelSize(R.dimen.group_top_item_width);
		int radius = context.getResources().getDimensionPixelSize(
				R.dimen.download_button_in_list_radius);

		Drawable d = getLoadingDrawable(context, w, h, radius,
				R.drawable.default_loading_icon_small);
		if (sGroupTop == null || sGroupTop.get() == null) {
			sGroupTop = new SoftReference<Drawable>(d);
		}
		return d;
	}

	public static DisplayImageOptions getBannerDisplayOption(Context context) {
		if (context == null) {
			return null;
		}
		Drawable d = getBannerDrawable(context);
		if (d == null) {
			return null;
		}
		return new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
				.resetViewBeforeLoading(false).showImageOnLoading(d).build();
	}

	private static Drawable getBannerDrawable(Context context) {
		if (sBanner != null && sBanner.get() != null) {
			// LogUtil.d(TAG, "list icon hit");
			return sBanner.get();
		}

		int h = context.getResources().getDimensionPixelSize(R.dimen.banner_height);
		int w = context.getResources().getDisplayMetrics().widthPixels;

		Drawable d = getLoadingDrawable(context, w, h, 0, R.drawable.banner_loading_icon);
		if (sBanner == null || sBanner.get() == null) {
			sBanner = new SoftReference<Drawable>(d);
		}
		return d;
	}

	public static DisplayImageOptions getApkImageDisplayOption(Context context, int w, int h) {
		if (context == null) {
			return null;
		}

		Drawable d = getApkImageDrawable(context, w, h);
		if (d == null) {
			return null;
		}
		return new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
				.resetViewBeforeLoading(false).showImageOnLoading(d).build();
	}

	private static Drawable getApkImageDrawable(Context context, int w, int h) {
		if (sApkImage != null && sApkImage.get() != null) {
			return sApkImage.get();
		}

		Drawable d = getLoadingDrawable(context, w, h, 0, R.drawable.default_loading_icon);
		if (sApkImage == null || sApkImage.get() == null) {
			sApkImage = new SoftReference<Drawable>(d);
		}
		return d;
	}

	private static Drawable getLoadingDrawable(Context context, int w, int h, int radius,
			int iconRes) {
		Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		Paint paint = new Paint();
		paint.setColor(0x88DDDDDD);
		canvas.drawRoundRect(new RectF(0, 0, w, h), radius, radius, paint);
		Bitmap icon = BitmapFactory.decodeResource(context.getResources(), iconRes);
		canvas.drawBitmap(icon, (w - icon.getWidth()) / 2, (h - icon.getHeight()) / 2, paint);
		return new BitmapDrawable(context.getResources(), output);
	}
}